import re
import time

# 日志文件路径
log_file1 = "D:/k12日志分析测试/1.log"
log_file1 = "D:/k12日志分析测试/newk12_center.log"

ip_count_dict = dict()

# ip_pattern = r'^(\d+\.){3}\d+'
ip_pattern = r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
ip_regex = re.compile(ip_pattern)


# 读取日志文件
def main():
    # 读取日志文件
    start = time.time()
    with open(log_file1, 'r', encoding='utf-8') as f:
        for line in f:
            mo = ip_regex.search(line)
            if mo:
                key = mo.group()
                ip_count_dict[key] = ip_count_dict.get(key, 0) + 1
    print(ip_count_dict)
    end = time.time()
    print('Running time: %.2f Seconds' % (end - start))


if __name__ == '__main__':
    main()
